package com.kx.kxblog.config;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.servlet.ServletContext;

@Configuration
@ConfigurationProperties(prefix = "kxblog")
@Data
@Slf4j
public class KxBlogProperties {
    private String name;
    private String version;
    private String tokenName;
    private String webLocation;

    private ServletContext application;
    public ServletContext getApplication() {
        return application;
    }
    @Autowired
    public void setApplication(ServletContext application) {
        this.application = application;
    }
    @PostConstruct
    public void postConstruct() throws Exception {
        application.setAttribute("version", version);
        application.setAttribute("contextPath", application.getContextPath());
        application.setAttribute("name", name);
        application.setAttribute("tokenName", tokenName);
        application.setAttribute("webLocation", webLocation);
        log.info("{}V{} 系统已启动！",name, version);
    }

    @PreDestroy
    public void exit() {
        log.error("{}V{} 系统已退出！",name, version);
    }
}
